//
//  MixTypeViewController.m
//  ObjectCProject
//
//  Created by lvfeijun on 2021/5/24.
//

#import "MixTypeViewController.h"
#import <WebKit/WebKit.h>

@interface MixTypeViewController ()

@property (nonatomic,strong) WKWebView *wkWebView;

@end

@implementation MixTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.wkWebView];
}

- (WKWebView *)wkWebView{
    if (!_wkWebView) {
        
        NSMutableString *js = [NSMutableString stringWithString:@"document.body.innerHTML ='<div>"];
        
        NSURL *url=[[NSBundle mainBundle]  URLForResource:@"json" withExtension:@"json"];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        NSError *error;
        NSArray *array = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
            for (int i=0; i<array.count; i++) {
                NSDictionary *dic =array[i];
                if ([dic[@"type"] isEqualToString:@"txt"]) {
                    [js appendFormat:@"<p>%@</p>",dic[@"content"]];
                }else if ([dic[@"type"] isEqualToString:@"image"]) {
                    [js appendFormat:@"<img src=\"%@\" style=\"width:100%% \">",dic[@"url"]];
                }else if ([dic[@"type"] isEqualToString:@"voice"]) {
                    [js appendFormat:@"<audio src=\"%@\" controls=\"controls\" autoplay=\"autoplay\" id=\"music\"></audio>",dic[@"url"]];
                }else if ([dic[@"type"] isEqualToString:@"video"]) {
                    [js appendFormat:@"<video src=\"%@\" controls=\"controls\" autoplay=\"autoplay\" id=\"video\"></video>",dic[@"url"]];
                }else if ([dic[@"type"] isEqualToString:@"file"]) {
                    [js appendFormat:@"<a> src=\"%@\"</a>",dic[@"text"]];
                }
            }
        
        [js appendString:@"</div>'"];
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        
        // 创建设置对象
        WKPreferences *preference = [[WKPreferences alloc]init];
        // 设置字体大小(最小的字体大小)
        preference.minimumFontSize = 15;
        // 设置偏好设置对象
        wkWebConfig.preferences = preference;
        WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 480, 750) configuration:wkWebConfig];
        webView.scrollView.alwaysBounceVertical = NO;
        _wkWebView = webView;
        NSURL *url1 = [[NSBundle mainBundle]  URLForResource:@"demo" withExtension:@"html"];
        [_wkWebView loadRequest:[NSURLRequest requestWithURL:url1]];
//        _wkWebView.frame = self.view.frame;
    }
    return _wkWebView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
